import 'package:dio/dio.dart';
import 'package:sinosheets/utils/storage/LSUtil.dart';

class TokenInterceptor extends Interceptor {
  final Map<String, String> _headers = {};

  Map<String, String> get headers => Map.unmodifiable(_headers);

  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    if (LSUtil.getLoginStatus()) {
      const key = "Authorization";
      final value = LSUtil.getTokenAll();
      options.headers[key] = value;
      _headers[key] = value;
    } else {
      options.headers.remove("Authorization");
      _headers.remove("Authorization");
    }
    handler.next(options);
  }

  /// 添加请求头
  void addHeader(String key, String value) {
    _headers[key] = value;
  }

  /// 移除请求头
  void removeHeader(String key) {
    _headers.remove(key);
  }

  /// 清除所有请求头
  void clearHeaders() {
    _headers.clear();
  }

  /// 获取指定请求头
  String? getHeader(String key) {
    return _headers[key];
  }
}
